n = int(input())
m = 0
if len(str(n)) == 4:
    for i in range(2, n + 1):
        if str(i) in str(n):
            f = 0
            for l in range(2, i // 2 + 1):
                if i % l == 0:
                    f = 1
                    break
            if f == 1:
                continue
            else:
                m = max(m, i)
print(m)

